package bth.reconfs;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

public class Hotels extends ActionBarActivity{

	private String[][] hotelInfos = new String[MainActivity.NB_MAX_LIST][2];
	private Bitmap[][] hotelIcon = new Bitmap[MainActivity.NB_MAX_LIST][1];
	private ActionBar actionBar;
	private ListView list;

	@SuppressLint("InlinedApi")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(android.R.layout.list_content);
		
		actionBar = getSupportActionBar();
		actionBar.setHomeButtonEnabled(true);
		list = (ListView) findViewById(android.R.id.list);
		
		hotelInfos[0][0] = "Scandic";
		hotelInfos[0][1] = "scandichotels.com/Hotels/Sweden/Karlskrona/Scandic-Karlskrona/";
		hotelIcon[0][0] = BitmapFactory.decodeResource(getResources(),R.drawable.logo_scandic);
		
		hotelInfos[1][0] = "Hotel Clarion Carlscrona";
		hotelInfos[1][1] = "nordicchoicehotels.com/clarion-collection/clarion-collection-hotel-carlscrona/";
		hotelIcon[1][0] = BitmapFactory.decodeResource(getResources(),R.drawable.logo_clarion);
		
		hotelInfos[2][0] = "First Hotel Statt";
		hotelInfos[2][1] = "firsthotels.com/Our-hotels/Hotels-in-Sweden/Karlskrona/First-Hotel-Statt-Karlskrona/";
		hotelIcon[2][0] = BitmapFactory.decodeResource(getResources(),R.drawable.logo_first);
		
		hotelInfos[3][0] = "First Hotel Ja";
		hotelInfos[3][1] = "firsthotels.com/Our-hotels/Hotels-in-Sweden/Karlskrona/First-Hotel-Ja/";
		hotelIcon[3][0] = BitmapFactory.decodeResource(getResources(),R.drawable.logo_first);
		
		hotelInfos[4][0] = "Hotel Siesta";
		hotelInfos[4][1] = "hotellsiesta.com";
		hotelIcon[4][0] = BitmapFactory.decodeResource(getResources(),R.drawable.logo_siesta);
		
		hotelInfos[5][0] = "Hotel Conrad";
		hotelInfos[5][1] = "hotelconrad.se/en/hotel-karlskrona/";
		hotelIcon[5][0] = BitmapFactory.decodeResource(getResources(),R.drawable.logo_conrad);
		
		hotelInfos[6][0] = "Hotel Aston";
		hotelInfos[6][1] = "hotelconrad.se/en/hotel-aston/";
		hotelIcon[6][0] = BitmapFactory.decodeResource(getResources(),R.drawable.logo_aston);

		ArrayList<HashMap<String, Object>> appItemList= new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < hotelInfos.length; i++) {
			if( hotelInfos[i][0] != null ) {
				appItemList.add( fillHashMap(
						hotelInfos[i][0],
						hotelInfos[i][1],
						hotelIcon[i][0]
						));
			}
		}

		// SimpleAdapter qui met en correspondance les items presents dans la list avec ceux de la vue
		SimpleAdapter adapter = new SimpleAdapter (this, appItemList, R.layout.list_item_image_right,
				new String[] {"Title", "Summary", "Image"},
				new int[] {R.id.list_item_title, R.id.list_item_resume, R.id.list_item_image});

		adapter.setViewBinder(new ViewBinder(){  
			public boolean setViewValue(View view, Object data, String textRepresentation)
			{
				if( (view instanceof ImageView) && (data instanceof Bitmap) )
				{
					ImageView iv = (ImageView) view;
					Bitmap bm = (Bitmap) data;
					iv.setImageBitmap(bm);
					return true;
				}
				return false;
			}
		});
		
		list.setAdapter(adapter);
	}

	// Remplir un HashMap
	private HashMap<String, Object> fillHashMap(String title, String summary, Object icon){
		HashMap<String, Object> item = new HashMap<String, Object>();
		item.put("Title", title);
		item.put("Summary", summary);
		item.put("Image", icon);
		return item;
	}
}